<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>Network Address Functions and Operators</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 9.1.2 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="Functions and Operators"
HREF="functions.html"><LINK
REL="PREVIOUS"
TITLE="Geometric Functions and Operators"
HREF="functions-geometry.html"><LINK
REL="NEXT"
TITLE="Text Search Functions and Operators"
HREF="functions-textsearch.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2011-12-01T22:07:59"></HEAD
><BODY
CLASS="SECT1"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
><A
HREF="index.html"
>PostgreSQL 9.1.2 Documentation</A
></TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
TITLE="Geometric Functions and Operators"
HREF="functions-geometry.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="functions.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
>Chapter 9. Functions and Operators</TD
><TD
WIDTH="20%"
ALIGN="right"
VALIGN="top"
><A
TITLE="Text Search Functions and Operators"
HREF="functions-textsearch.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="FUNCTIONS-NET"
>9.12. Network Address Functions and Operators</A
></H1
><P
>   <A
HREF="functions-net.html#CIDR-INET-OPERATORS-TABLE"
>Table 9-33</A
> shows the operators
   available for the <TT
CLASS="TYPE"
>cidr</TT
> and <TT
CLASS="TYPE"
>inet</TT
> types.
   The operators <TT
CLASS="LITERAL"
>&lt;&lt;</TT
>,
   <TT
CLASS="LITERAL"
>&lt;&lt;=</TT
>, <TT
CLASS="LITERAL"
>&gt;&gt;</TT
>, and
   <TT
CLASS="LITERAL"
>&gt;&gt;=</TT
> test for subnet inclusion.  They
   consider only the network parts of the two addresses (ignoring any
   host part) and determine whether one network is identical to
   or a subnet of the other.
  </P
><DIV
CLASS="TABLE"
><A
NAME="CIDR-INET-OPERATORS-TABLE"
></A
><P
><B
>Table 9-33. <TT
CLASS="TYPE"
>cidr</TT
> and <TT
CLASS="TYPE"
>inet</TT
> Operators</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><COL><COL><COL><THEAD
><TR
><TH
>Operator</TH
><TH
>Description</TH
><TH
>Example</TH
></TR
></THEAD
><TBODY
><TR
><TD
> <TT
CLASS="LITERAL"
>&lt;</TT
> </TD
><TD
>is less than</TD
><TD
><TT
CLASS="LITERAL"
>inet '192.168.1.5' &lt; inet '192.168.1.6'</TT
></TD
></TR
><TR
><TD
> <TT
CLASS="LITERAL"
>&lt;=</TT
> </TD
><TD
>is less than or equal</TD
><TD
><TT
CLASS="LITERAL"
>inet '192.168.1.5' &lt;= inet '192.168.1.5'</TT
></TD
></TR
><TR
><TD
> <TT
CLASS="LITERAL"
>=</TT
> </TD
><TD
>equals</TD
><TD
><TT
CLASS="LITERAL"
>inet '192.168.1.5' = inet '192.168.1.5'</TT
></TD
></TR
><TR
><TD
> <TT
CLASS="LITERAL"
>&gt;=</TT
> </TD
><TD
>is greater or equal</TD
><TD
><TT
CLASS="LITERAL"
>inet '192.168.1.5' &gt;= inet '192.168.1.5'</TT
></TD
></TR
><TR
><TD
> <TT
CLASS="LITERAL"
>&gt;</TT
> </TD
><TD
>is greater than</TD
><TD
><TT
CLASS="LITERAL"
>inet '192.168.1.5' &gt; inet '192.168.1.4'</TT
></TD
></TR
><TR
><TD
> <TT
CLASS="LITERAL"
>&lt;&gt;</TT
> </TD
><TD
>is not equal</TD
><TD
><TT
CLASS="LITERAL"
>inet '192.168.1.5' &lt;&gt; inet '192.168.1.4'</TT
></TD
></TR
><TR
><TD
> <TT
CLASS="LITERAL"
>&lt;&lt;</TT
> </TD
><TD
>is contained within</TD
><TD
><TT
CLASS="LITERAL"
>inet '192.168.1.5' &lt;&lt; inet '192.168.1/24'</TT
></TD
></TR
><TR
><TD
> <TT
CLASS="LITERAL"
>&lt;&lt;=</TT
> </TD
><TD
>is contained within or equals</TD
><TD
><TT
CLASS="LITERAL"
>inet '192.168.1/24' &lt;&lt;= inet '192.168.1/24'</TT
></TD
></TR
><TR
><TD
> <TT
CLASS="LITERAL"
>&gt;&gt;</TT
> </TD
><TD
>contains</TD
><TD
><TT
CLASS="LITERAL"
>inet '192.168.1/24' &gt;&gt; inet '192.168.1.5'</TT
></TD
></TR
><TR
><TD
> <TT
CLASS="LITERAL"
>&gt;&gt;=</TT
> </TD
><TD
>contains or equals</TD
><TD
><TT
CLASS="LITERAL"
>inet '192.168.1/24' &gt;&gt;= inet '192.168.1/24'</TT
></TD
></TR
><TR
><TD
> <TT
CLASS="LITERAL"
>~</TT
> </TD
><TD
>bitwise NOT</TD
><TD
><TT
CLASS="LITERAL"
>~ inet '192.168.1.6'</TT
></TD
></TR
><TR
><TD
> <TT
CLASS="LITERAL"
>&amp;</TT
> </TD
><TD
>bitwise AND</TD
><TD
><TT
CLASS="LITERAL"
>inet '192.168.1.6' &amp; inet '0.0.0.255'</TT
></TD
></TR
><TR
><TD
> <TT
CLASS="LITERAL"
>|</TT
> </TD
><TD
>bitwise OR</TD
><TD
><TT
CLASS="LITERAL"
>inet '192.168.1.6' | inet '0.0.0.255'</TT
></TD
></TR
><TR
><TD
> <TT
CLASS="LITERAL"
>+</TT
> </TD
><TD
>addition</TD
><TD
><TT
CLASS="LITERAL"
>inet '192.168.1.6' + 25</TT
></TD
></TR
><TR
><TD
> <TT
CLASS="LITERAL"
>-</TT
> </TD
><TD
>subtraction</TD
><TD
><TT
CLASS="LITERAL"
>inet '192.168.1.43' - 36</TT
></TD
></TR
><TR
><TD
> <TT
CLASS="LITERAL"
>-</TT
> </TD
><TD
>subtraction</TD
><TD
><TT
CLASS="LITERAL"
>inet '192.168.1.43' - inet '192.168.1.19'</TT
></TD
></TR
></TBODY
></TABLE
></DIV
><P
>   <A
HREF="functions-net.html#CIDR-INET-FUNCTIONS-TABLE"
>Table 9-34</A
> shows the functions
   available for use with the <TT
CLASS="TYPE"
>cidr</TT
> and <TT
CLASS="TYPE"
>inet</TT
>
   types.  The <CODE
CLASS="FUNCTION"
>abbrev</CODE
>, <CODE
CLASS="FUNCTION"
>host</CODE
>,
   and <CODE
CLASS="FUNCTION"
>text</CODE
>
   functions are primarily intended to offer alternative display
   formats.
  </P
><DIV
CLASS="TABLE"
><A
NAME="CIDR-INET-FUNCTIONS-TABLE"
></A
><P
><B
>Table 9-34. <TT
CLASS="TYPE"
>cidr</TT
> and <TT
CLASS="TYPE"
>inet</TT
> Functions</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><COL><COL><COL><COL><COL><THEAD
><TR
><TH
>Function</TH
><TH
>Return Type</TH
><TH
>Description</TH
><TH
>Example</TH
><TH
>Result</TH
></TR
></THEAD
><TBODY
><TR
><TD
>         
         <TT
CLASS="LITERAL"
><CODE
CLASS="FUNCTION"
>abbrev(<TT
CLASS="TYPE"
>inet</TT
>)</CODE
></TT
>
        </TD
><TD
><TT
CLASS="TYPE"
>text</TT
></TD
><TD
>abbreviated display format as text</TD
><TD
><TT
CLASS="LITERAL"
>abbrev(inet '10.1.0.0/16')</TT
></TD
><TD
><TT
CLASS="LITERAL"
>10.1.0.0/16</TT
></TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
><CODE
CLASS="FUNCTION"
>abbrev(<TT
CLASS="TYPE"
>cidr</TT
>)</CODE
></TT
></TD
><TD
><TT
CLASS="TYPE"
>text</TT
></TD
><TD
>abbreviated display format as text</TD
><TD
><TT
CLASS="LITERAL"
>abbrev(cidr '10.1.0.0/16')</TT
></TD
><TD
><TT
CLASS="LITERAL"
>10.1/16</TT
></TD
></TR
><TR
><TD
>         
         <TT
CLASS="LITERAL"
><CODE
CLASS="FUNCTION"
>broadcast(<TT
CLASS="TYPE"
>inet</TT
>)</CODE
></TT
>
        </TD
><TD
><TT
CLASS="TYPE"
>inet</TT
></TD
><TD
>broadcast address for network</TD
><TD
><TT
CLASS="LITERAL"
>broadcast('192.168.1.5/24')</TT
></TD
><TD
><TT
CLASS="LITERAL"
>192.168.1.255/24</TT
></TD
></TR
><TR
><TD
>         
         <TT
CLASS="LITERAL"
><CODE
CLASS="FUNCTION"
>family(<TT
CLASS="TYPE"
>inet</TT
>)</CODE
></TT
>
        </TD
><TD
><TT
CLASS="TYPE"
>int</TT
></TD
><TD
>extract family of address; <TT
CLASS="LITERAL"
>4</TT
> for IPv4,
         <TT
CLASS="LITERAL"
>6</TT
> for IPv6</TD
><TD
><TT
CLASS="LITERAL"
>family('::1')</TT
></TD
><TD
><TT
CLASS="LITERAL"
>6</TT
></TD
></TR
><TR
><TD
>         
         <TT
CLASS="LITERAL"
><CODE
CLASS="FUNCTION"
>host(<TT
CLASS="TYPE"
>inet</TT
>)</CODE
></TT
>
        </TD
><TD
><TT
CLASS="TYPE"
>text</TT
></TD
><TD
>extract IP address as text</TD
><TD
><TT
CLASS="LITERAL"
>host('192.168.1.5/24')</TT
></TD
><TD
><TT
CLASS="LITERAL"
>192.168.1.5</TT
></TD
></TR
><TR
><TD
>         
         <TT
CLASS="LITERAL"
><CODE
CLASS="FUNCTION"
>hostmask(<TT
CLASS="TYPE"
>inet</TT
>)</CODE
></TT
>
        </TD
><TD
><TT
CLASS="TYPE"
>inet</TT
></TD
><TD
>construct host mask for network</TD
><TD
><TT
CLASS="LITERAL"
>hostmask('192.168.23.20/30')</TT
></TD
><TD
><TT
CLASS="LITERAL"
>0.0.0.3</TT
></TD
></TR
><TR
><TD
>         
         <TT
CLASS="LITERAL"
><CODE
CLASS="FUNCTION"
>masklen(<TT
CLASS="TYPE"
>inet</TT
>)</CODE
></TT
>
        </TD
><TD
><TT
CLASS="TYPE"
>int</TT
></TD
><TD
>extract netmask length</TD
><TD
><TT
CLASS="LITERAL"
>masklen('192.168.1.5/24')</TT
></TD
><TD
><TT
CLASS="LITERAL"
>24</TT
></TD
></TR
><TR
><TD
>         
         <TT
CLASS="LITERAL"
><CODE
CLASS="FUNCTION"
>netmask(<TT
CLASS="TYPE"
>inet</TT
>)</CODE
></TT
>
        </TD
><TD
><TT
CLASS="TYPE"
>inet</TT
></TD
><TD
>construct netmask for network</TD
><TD
><TT
CLASS="LITERAL"
>netmask('192.168.1.5/24')</TT
></TD
><TD
><TT
CLASS="LITERAL"
>255.255.255.0</TT
></TD
></TR
><TR
><TD
>         
         <TT
CLASS="LITERAL"
><CODE
CLASS="FUNCTION"
>network(<TT
CLASS="TYPE"
>inet</TT
>)</CODE
></TT
>
        </TD
><TD
><TT
CLASS="TYPE"
>cidr</TT
></TD
><TD
>extract network part of address</TD
><TD
><TT
CLASS="LITERAL"
>network('192.168.1.5/24')</TT
></TD
><TD
><TT
CLASS="LITERAL"
>192.168.1.0/24</TT
></TD
></TR
><TR
><TD
>         
         <TT
CLASS="LITERAL"
><CODE
CLASS="FUNCTION"
>set_masklen(<TT
CLASS="TYPE"
>inet</TT
>, <TT
CLASS="TYPE"
>int</TT
>)</CODE
></TT
>
        </TD
><TD
><TT
CLASS="TYPE"
>inet</TT
></TD
><TD
>set netmask length for <TT
CLASS="TYPE"
>inet</TT
> value</TD
><TD
><TT
CLASS="LITERAL"
>set_masklen('192.168.1.5/24', 16)</TT
></TD
><TD
><TT
CLASS="LITERAL"
>192.168.1.5/16</TT
></TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
><CODE
CLASS="FUNCTION"
>set_masklen(<TT
CLASS="TYPE"
>cidr</TT
>, <TT
CLASS="TYPE"
>int</TT
>)</CODE
></TT
></TD
><TD
><TT
CLASS="TYPE"
>cidr</TT
></TD
><TD
>set netmask length for <TT
CLASS="TYPE"
>cidr</TT
> value</TD
><TD
><TT
CLASS="LITERAL"
>set_masklen('192.168.1.0/24'::cidr, 16)</TT
></TD
><TD
><TT
CLASS="LITERAL"
>192.168.0.0/16</TT
></TD
></TR
><TR
><TD
>         
         <TT
CLASS="LITERAL"
><CODE
CLASS="FUNCTION"
>text(<TT
CLASS="TYPE"
>inet</TT
>)</CODE
></TT
>
        </TD
><TD
><TT
CLASS="TYPE"
>text</TT
></TD
><TD
>extract IP address and netmask length as text</TD
><TD
><TT
CLASS="LITERAL"
>text(inet '192.168.1.5')</TT
></TD
><TD
><TT
CLASS="LITERAL"
>192.168.1.5/32</TT
></TD
></TR
></TBODY
></TABLE
></DIV
><P
>   Any <TT
CLASS="TYPE"
>cidr</TT
> value can be cast to <TT
CLASS="TYPE"
>inet</TT
> implicitly
   or explicitly; therefore, the functions shown above as operating on
   <TT
CLASS="TYPE"
>inet</TT
> also work on <TT
CLASS="TYPE"
>cidr</TT
> values.  (Where there are
   separate functions for <TT
CLASS="TYPE"
>inet</TT
> and <TT
CLASS="TYPE"
>cidr</TT
>, it is because
   the behavior should be different for the two cases.)
   Also, it is permitted to cast an <TT
CLASS="TYPE"
>inet</TT
> value to <TT
CLASS="TYPE"
>cidr</TT
>.
   When this is done, any bits to the right of the netmask are silently zeroed
   to create a valid <TT
CLASS="TYPE"
>cidr</TT
> value.
   In addition,
   you can cast a text value to <TT
CLASS="TYPE"
>inet</TT
> or <TT
CLASS="TYPE"
>cidr</TT
>
   using normal casting syntax: for example,
   <TT
CLASS="LITERAL"
>inet(<TT
CLASS="REPLACEABLE"
><I
>expression</I
></TT
>)</TT
> or
   <TT
CLASS="LITERAL"
><TT
CLASS="REPLACEABLE"
><I
>colname</I
></TT
>::cidr</TT
>.
  </P
><P
>   <A
HREF="functions-net.html#MACADDR-FUNCTIONS-TABLE"
>Table 9-35</A
> shows the functions
   available for use with the <TT
CLASS="TYPE"
>macaddr</TT
> type.  The function
   <TT
CLASS="LITERAL"
><CODE
CLASS="FUNCTION"
>trunc(<TT
CLASS="TYPE"
>macaddr</TT
>)</CODE
></TT
> returns a MAC
   address with the last 3 bytes set to zero.  This can be used to
   associate the remaining prefix with a manufacturer.
  </P
><DIV
CLASS="TABLE"
><A
NAME="MACADDR-FUNCTIONS-TABLE"
></A
><P
><B
>Table 9-35. <TT
CLASS="TYPE"
>macaddr</TT
> Functions</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><COL><COL><COL><COL><COL><THEAD
><TR
><TH
>Function</TH
><TH
>Return Type</TH
><TH
>Description</TH
><TH
>Example</TH
><TH
>Result</TH
></TR
></THEAD
><TBODY
><TR
><TD
>         
         <TT
CLASS="LITERAL"
><CODE
CLASS="FUNCTION"
>trunc(<TT
CLASS="TYPE"
>macaddr</TT
>)</CODE
></TT
>
        </TD
><TD
><TT
CLASS="TYPE"
>macaddr</TT
></TD
><TD
>set last 3 bytes to zero</TD
><TD
><TT
CLASS="LITERAL"
>trunc(macaddr '12:34:56:78:90:ab')</TT
></TD
><TD
><TT
CLASS="LITERAL"
>12:34:56:00:00:00</TT
></TD
></TR
></TBODY
></TABLE
></DIV
><P
>    The <TT
CLASS="TYPE"
>macaddr</TT
> type also supports the standard relational
    operators (<TT
CLASS="LITERAL"
>&gt;</TT
>, <TT
CLASS="LITERAL"
>&lt;=</TT
>, etc.) for
    lexicographical ordering.
   </P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="functions-geometry.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="functions-textsearch.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Geometric Functions and Operators</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="functions.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Text Search Functions and Operators</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>